package com.mvc.cms.manager.assist.impl;

import java.io.File;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mvc.cms.dao.assist.CmsHouseDataDao;
import com.mvc.cms.entity.assist.CmsHouseData;
import com.mvc.cms.manager.assist.CmsHouseDataMng;
import com.mvc.common.page.Pagination;
@Service
@Transactional
public class CmsHouseDataMngImpl implements CmsHouseDataMng{

	private CmsHouseDataDao dao;
	@Autowired
	public void setDao(CmsHouseDataDao dao) {
		this.dao = dao;
	}

	public CmsHouseData save(CmsHouseData bean) {
		
		return dao.save(bean);
	}

	public Pagination getPage(String name, String area, String seller,String license,
			String use, String building, Date begin, Date end,int pageNo, int pageSize) {
		   Pagination page = dao.getPage(name, area, seller, license, use, 
				building, begin, end, pageNo, pageSize);
		return page;
	}
	
	public void ReadExcel(String path,String name){
		Workbook book;
		try {
			File file = new File(path);
			WorkbookSettings workbookSettings = new WorkbookSettings();
			workbookSettings.setEncoding("UTF-8");
			book = Workbook.getWorkbook(file, workbookSettings);
			Sheet sheet = book.getSheet(0);
			List<Map<String, String>> list = new ArrayList<Map<String, String>>();
			for (int i = 0; i < sheet.getRows(); i++) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("name", sheet.getCell(0, i).getContents());
				map.put("area", sheet.getCell(1, i).getContents());
				map.put("seller", sheet.getCell(2, i).getContents());
				map.put("license", sheet.getCell(3, i).getContents());
				map.put("use", sheet.getCell(4, i).getContents());
				map.put("building", sheet.getCell(5, i).getContents());
				map.put("covered", sheet.getCell(6, i).getContents());
				map.put("sets", sheet.getCell(7, i).getContents());
				map.put("share", sheet.getCell(8, i).getContents());
				map.put("turnover", sheet.getCell(9, i).getContents());
				map.put("surface", sheet.getCell(10, i).getContents());
				map.put("sleeve", sheet.getCell(11, i).getContents());
				map.put("contractTime", sheet.getCell(12, i).getContents());
				list.add(map);
			}
			this.save(list,name);
			book.close(); 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void save(List<Map<String, String>> list,String name) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(Map<String, String> m:list){
			CmsHouseData bean = new CmsHouseData();
			bean.setName(m.get("name"));
			bean.setArea(m.get("area"));
			bean.setSeller(m.get("seller"));
			bean.setLicense(m.get("license"));
			bean.setUse(m.get("use"));
			bean.setBuilding(m.get("building"));
			bean.setCovered(Float.parseFloat(m.get("covered")));
			bean.setSets(Float.parseFloat(m.get("sets")));
			bean.setShare(Float.parseFloat(m.get("share")));
			bean.setTurnover(Float.parseFloat(m.get("turnover")));
			bean.setSurface(Float.parseFloat(m.get("surface")));
			bean.setSleeve(Float.parseFloat(m.get("sleeve")));
			bean.setCreateTime(new Timestamp(System.currentTimeMillis()));
			bean.setAdmin(name);
			try {
				bean.setContractTime(sdf.parse(m.get("contractTime")));
			} catch (ParseException e) {
				System.out.println("excel 导入时间格式错误!");
			}
			dao.save(bean);
		}
	}

	public CmsHouseData find(Integer id) {
		return dao.find(id);
	}
	
	public CmsHouseData[] delete(Integer[] ids) {
		CmsHouseData[] beans = new CmsHouseData[ids.length];
		for(int i=0;i< ids.length;i++){
			beans[i] = dao.delete(ids[i]);
		}
		return beans;
	}

	public List<Object[]> rangeStatistics(String name,String are,String seller,
	        String license,String use,String building,Date begin, Date end) {
		return dao.rangeStatistics(name, are, seller, license, use, building, begin, end);
	}

	public List<Object[]> propertyStatistics(String name,String are,String seller,
	        String license,String use,String building,Date begin, Date end) {
		return dao.propertyStatistics(name, are, seller, license, use, building, begin, end);
	}
	@Override
	public List<String> getAllRow(String key) {
		// TODO Auto-generated method stub
		return dao.getAllRow(key);
	}
}
